/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlparser;

/**
 *
 * @author pbelov
 */
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import base.*;
import java.util.Vector;

public class bugParser extends DefaultHandler{
    Bug bug = new Bug();

    Vector vc = new Vector();
    Comment tcomment;

    String thisElement = "";

    public static String DBG = "[DBG]";
    public static String ID = "id";
    public static String TITLE = "title";
    public static String ASSIGNEE = "assignee";
    public static String CREATOR = "creator";
    public static String TESTER = "tester";
    public static String DESC = "desc";
    public static String BUGVER = "bug_ver";
    public static String FIXVER = "fix_ver";
    public static String FIX = "fix_number";
    public static String PROJECT = "project";
    public static String DEVICE = "device";
    public static String COMMENT = "comment";
    public static String PRIRITY = "priority";
    public static String STATE = "state";
    public static String TEXT = "text";
    public static String AUTHOR = "author";
    public static String REOPEN = "reopen";

    public bugParser() {
        vc.setSize(0);
    }

    public Bug getResult(){
        return bug;
    }

    public int getCommentsCount() {
        return bug.getCommentsCount();
    }

    @Override
    public void startDocument() throws SAXException {
//        System.out.println("Start parse Bug XML...");
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
        thisElement = qName;
        if (thisElement.equals(COMMENT)) {
            tcomment = new Comment();
        }
    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
        if (qName.equals(COMMENT)) {
            bug.addComment(tcomment);
            tcomment = null;
        }
        
        thisElement = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (thisElement.equals(ID)) {
            bug.setID(new Integer(new String(ch, start, length)));
        }

        if (thisElement.equals(TITLE)) {
            bug.setTitle(new String(ch, start, length));
        }

        if (thisElement.equals(DESC)) {
            bug.setDescription(new String(ch, start, length));
        }

        if (thisElement.equals(ASSIGNEE)) {
            bug.setAssignee(new String(ch, start, length));
        }

        if (thisElement.equals(CREATOR)) {
            bug.setCreator(new String(ch, start, length));
        }

        if (thisElement.equals(TESTER)) {
            bug.setTester(new String(ch, start, length));
        }

        if (thisElement.equals(DEVICE)) {
            bug.setDevice(new String(ch, start, length));
        }

        if (thisElement.equals(PROJECT)) {
            bug.setProject(new String(ch, start, length));
        }

        if (thisElement.equals(PRIRITY)) {
            bug.setPriority(new Integer(new String(ch, start, length)));
        }

        if (thisElement.equals(STATE)) {
            bug.setState(new Integer(new String(ch, start, length)));
        }

        if (thisElement.equals(PROJECT)) {
            bug.setProject(new String(ch, start, length));
        }

        if (thisElement.equals(BUGVER)) {
            bug.setBugVer(new String(ch, start, length));
        }

        if (thisElement.equals(FIXVER)) {
            bug.setFixVer(new String(ch, start, length));
        }

        if (thisElement.equals(FIX)) {
            bug.setFixNumber(new Integer(new String(ch, start, length)));
        }

        if (thisElement.equals(AUTHOR)) {
            tcomment.setAuthor(new String(ch, start, length));
        }

        if (thisElement.equals(TEXT)) {
            tcomment.setText(new String(ch, start, length));
        }

        if (thisElement.equals(REOPEN)) {
            bug.setReopenCount(new Integer(new String(ch, start, length)));
        }
    }

    @Override
    public void endDocument() {
//      System.out.println("Stop parse Bug XML...");
    }
}

